//package com.wanwu.channel.service.strategy.handle.allianpay;
//
//
//import com.wanwu.channel.common.enums.ClearStatusEnum;
//import com.wanwu.channel.common.enums.ExceptionEnum;
//import com.wanwu.channel.common.exception.SystemException;
//import com.wanwu.channel.common.util.AmountUtil;
//import com.wanwu.channel.common.util.LogUtil;
//import com.wanwu.channel.core.ClearService;
//import com.wanwu.channel.dao.entity.ChannelClearEntity;
//import com.wanwu.channel.remote.allinpay.TLHttp;
//import com.wanwu.channel.remote.allinpay.TLHttpUtils.TLApplyStat;
//import com.wanwu.channel.remote.allinpay.TLHttpUtils.TLRspCodx;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//
//import java.math.BigDecimal;
//
//import static com.wanwu.channel.remote.allinpay.TLHttp.*;
//
///**
// * @author JiWen
// * @time 2020/1/21 15:21
// * 3. 查询申请单状态 （返回待确认付款）
// * 4. 锁汇查询 执行申请单
// * 5. 查询申请单状态 (返回返回终态)
// */
//@Service("applyQueryHandler")
//public class ApplyQueryHandler implements Handler {
//
//    public static Logger logger = LoggerFactory.getLogger(ApplyQueryHandler.class);
//    @Resource
//    private ClearService clearService;
//
//    @Override
//    public void handle(AliianPayContext context) {
//        ChannelClearEntity clearEntity = context.getClearEntity();
//        String ext2 = clearEntity.getExt2();
//        StepEnum currentStep = StepEnum.getByCode(Integer.parseInt(ext2));
//        if (StepEnum.isNotQuery(currentStep)) {
//            return;
//        }
//        try {
//            ApplyQryReq req = toApplyQuery(clearEntity);
//            CashApplyResult resp = (CashApplyResult) TLHttp.request(BizTypeEnum.APPLY_QUERY, req);
//            if (TLRspCodx.REQ_SUCC.equals(resp.getRspcode())) {
//                String state = resp.getState();
//                if (currentStep == StepEnum.APPLY_QUERY1) {
//                    if ("03".equals(state)) {
//                        clearEntity.setExt2(String.valueOf(StepEnum.EXEC_APPLY.getCode()));
//                    }
//                    if (TLApplyStat.isSuccess(state)) {
//                        clearEntity.setExt2(String.valueOf(StepEnum.FINISH.getCode()));
//                    }
//                    if (TLApplyStat.isFail(state)) {
//                        clearEntity.setClearStatus(ClearStatusEnum.FAIL.getStatus());
//                    }
//                    if (TLApplyStat.isSuccess(state)) {
//                        clearEntity.setClearStatus(ClearStatusEnum.SUCCESS.getStatus());
//                        BigDecimal payamount = resp.getPayamount();
//                        clearEntity.setAmount(AmountUtil.yuan2cent(payamount));
//                        clearEntity.setCurrency(resp.getPayccy());
//                        BigDecimal stlamount = resp.getStlamount();
//                        clearEntity.setSettAmount(AmountUtil.yuan2cent(stlamount));
//                        clearEntity.setSettCurrency(resp.getStlccy());
//                        BigDecimal fee = resp.getFee();
//                        clearEntity.setFee(AmountUtil.yuan2cent(fee));
//                        clearEntity.setExRate(resp.getFee());
//                    }
//                    clearEntity.setThirdRespCode(resp.getState());
//                    clearService.updateChannelClear(clearEntity);
//                }
//            } else {
//                clearEntity.setThirdRespCode(resp.getRspcode());
//                clearEntity.setThirdRespMsg(resp.getRspinfo());
//                clearService.updateChannelClear(clearEntity);
//            }
//        } catch (Exception e) {
//            LogUtil.error(logger, e, "查询申请单异常 channelNo: {0}", clearEntity.getChannelNo());
//            throw new SystemException(ExceptionEnum.E10021);
//        }
//    }
//
//    private ApplyQryReq toApplyQuery(ChannelClearEntity clearEntity) {
//        ApplyQryReq req = new ApplyQryReq();
//        req.setMeraplid(clearEntity.getSerialNo());
//        req.setApplyId(clearEntity.getExt1());
//        return req;
//    }
//}
